

public class Calculator {
	String a = new String();
	DataStack datastack;
	CharStack charstack;
	void Getstack(){
		datastack = new DataStack(a.length());
		datastack.getStack();
		charstack = new CharStack(a.length());
		charstack.getStack();
	}
	Rational manage() {
		int i=0;
		Rational r = new Rational();
		double d;
		while(true) {
			if(i>=a.length())
				break;
			char c = a.charAt(i);
			if(c>='0' && c<='9') {
				String temp = String.valueOf(c);
				String s = new String();
				s = s+temp;
				i++;
				if(i>=a.length()) {
					d = Double.parseDouble(s);
					r.setNumerator(d);
					datastack.push(r);
					break;
				}
				c = a.charAt(i);
				while(c>='0' && c<='9') {
					temp = String.valueOf(c);
					s = s+temp;
					i++;
					if(i>=a.length())
						break;
					c = a.charAt(i);
				}
				d = Double.parseDouble(s);
				r.setNumerator(d);
				datastack.push(r);
				continue;
			}
			else if(c=='*' || c=='/') {
				while(true) {
					if(charstack.top==-1) {
						charstack.push(c);
						break;
					}
					if(charstack.a[charstack.top]=='+' || charstack.a[charstack.top]=='-') {
						charstack.push(c);
						break;
					}
					else if((charstack.a[charstack.top]=='*' || charstack.a[charstack.top]=='/') && (datastack.top>=1)) {
						calculate(charstack.a[charstack.top]);
						continue;
					}
					else {
						charstack.push(c);
						break;
					}
				}
			}
			else if(c=='+' || c=='-') {
				while(true) {
					if(charstack.top==-1) {
						charstack.push(c);
						break;
					}						
					else if((charstack.a[charstack.top]=='+' || charstack.a[charstack.top]=='-' || charstack.a[charstack.top]=='*' || charstack.a[charstack.top]=='/') && (datastack.top>=1)) {
						calculate(charstack.a[charstack.top]);
						continue;
					}
					else {
						charstack.push(c);
						break;
					}
				}
				
			}
			else if(c=='(') {
				charstack.push(c);
			}
			else if(c==')') {
				while(true) {
					if(charstack.a[charstack.top]=='(') {
						charstack.pop();
						break;
					}
					else {
						calculate(charstack.a[charstack.top]);
					}
				}
			}
			i++;
		}
		while(true) {
			if(datastack.top>=1)
				calculate(charstack.a[charstack.top]);
			else
				break;
		}
		return datastack.pop();
	}
	void calculate(char c) {
		Rational a,b;
		charstack.pop();
		b = datastack.pop();
		a = datastack.pop();
		switch(c) {
			case '+':
				datastack.push(a.add(b));
				break;
			case '-':
				datastack.push(a.sub(b));
				break;
			case '*':
				datastack.push(a.muti(b));
				break;
			case '/':
				datastack.push(a.div(b));
				break;
		}
	}
	
}
